package com.fingard.dsp.bank.dbobj.receipt.ownact;

import java.sql.Connection;
import java.util.Date;
import java.util.Map;

import com.fingard.FGBiz;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.helpers.dbhelpers.InsertHelper;
import com.fingard.dsp.bank.helpers.dbhelpers.LoadHelper;

public class OwnActHelper {

	public OwnActHelper(){
		
	}
	
	/**不存在则新增--回单查询的本方账号信息*/
	public int insertIfNotExist(OwnActData pData) throws Exception{
		pData.createdOn = new Date();
		Connection conn = null;
		try{
			conn = FGBiz.getLocalDbConn();
			String chkSql = "select count(*) from ACTQRYRECEIPT where ACTNUM=? and BANKCODE=?";
			Object[] objs = new Object[2];
			objs[0] = pData.actNum;
			objs[1] = pData.bankCode;
			Object chkObj = JDBCHelper.execScalar(conn, chkSql, objs);
			int chkCount = -1;
			if(chkObj != null){
				chkCount = Integer.parseInt(chkObj.toString());
			}
			if(chkCount <= 0){
				int rowAff = InsertHelper.insertObject(conn, "ACTQRYRECEIPT", pData);
				if(rowAff == 1){
					return 1;
				}else{
					throw new Exception("写入ACTQRYRECEIPT的影响行数："+rowAff);
				}
			}
		}catch(Exception ex){
			throw(ex);
		}finally{
			if(conn != null){
				try{
					conn.close();
				}catch(Exception ex){
					throw(ex);
				}
			}
		}
		return -1;
	}
	
	/**加载--回单查询的本方账号信息*/
	public OwnActData loadOwnActData(String pActNum) throws Exception{
		Connection conn = null;
		try{
			conn = FGBiz.getLocalDbConn();
			String sql = "select * from ACTQRYRECEIPT where ACTNUM=?";
			Object[] objs = new Object[1];
			objs[0] = pActNum;
			Map<String,Object> mapData = JDBCHelper.execQuerySingle(conn, sql, objs);
			if(mapData != null && mapData.size() > 0){
				OwnActData respData = new OwnActData();
				LoadHelper.loadObject(mapData, respData);
				return respData;
			}
		}catch(Exception ex){
			throw(ex);
		}finally{
			if(conn != null){
				try{
					conn.close();
				}catch(Exception ex){
					throw(ex);
				}
			}
		}
		return null;
	}
}
